package com.tuanbiao.ui.setting;

import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;

import com.hzjq.data.hzjq.entity.AppDataEntity;
import com.tuanbiao.AppConfig;
import com.tuanbiao.R;
import com.tuanbiao.base.mvp.BasePresenterActivity;
import com.tuanbiao.controllers.AboutController;
import com.tuanbiao.databinding.ActivityAboutBinding;
import com.tuanbiao.dialog.CustomProgressDialog;
import com.tuanbiao.dialog.MessageDialog;
import com.tuanbiao.keyevent.KeyEventHelper;
import com.tuanbiao.models.AboutModel;
import com.tuanbiao.presenter.AboutPresenter;
import com.tuanbiao.utils.ClickUtil;

public class AboutActivity extends BasePresenterActivity<AboutPresenter, ActivityAboutBinding> implements AboutController.View {
    private CustomProgressDialog.Builder dialogBuilder;
    private MessageDialog.Builder updateSuccessDialog;

    public static void start(Context context) {
        Intent starter = new Intent(context, AboutActivity.class);
        context.startActivity(starter);
    }

    @Override
    protected ActivityAboutBinding getBindingView() {
        return ActivityAboutBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void initView() {
        if (AppConfig.isDebug()) {
//            binding.tvBuildTime.setVisibility(View.VISIBLE);
        }
        binding.ivToolbarBack.setOnClickListener(getBackListener());
        binding.sbAppUpdate.setOnClickListener(view -> {
            if (ClickUtil.isFastClick()) {
                return;
            }
            mPresenter.checkAppUpdate();
        });
        binding.sbFirmwareUpdate.setOnClickListener(view -> {
            if (ClickUtil.isFastClick()) {
                return;
            }
            showUpdateBinDialog();
        });
    }

    @Override
    protected AboutPresenter createPresenter() {
        return new AboutPresenter(new AboutModel(), this);
    }


    @Override
    public void showVersion(String firmwareVersion, String appVersion, String buildType) {
        binding.tvFirmwareVersion.setText(getString(R.string.about_firmware_version, firmwareVersion));
        binding.tvAppVersion.setText(getString(R.string.about_app_version, appVersion));
        binding.tvBuildType.setText(buildType);
    }

    @Override
    public void showFirmwareUpdate(boolean update) {
        binding.tvCanFirmwareUpdate.setVisibility(update ? View.VISIBLE : View.GONE);
    }

    @Override
    public void showAppUpdate(boolean update) {
        binding.tvCanAppUpdate.setVisibility(update ? View.VISIBLE : View.GONE);
    }

    @Override
    public void showProgressDialog(int progress, String msg) {
        if (null == dialogBuilder) {
            dialogBuilder = new CustomProgressDialog.Builder(getActivity());
            dialogBuilder.setProgress(0);
        }
        dialogBuilder.setMessage(msg);
        dialogBuilder.setProgress(progress);
        if (!dialogBuilder.isShowing())
            dialogBuilder.show();
        if (progress >= 100) {
            dialogBuilder.dismiss();
            if (null == updateSuccessDialog) {
                updateSuccessDialog = new MessageDialog.Builder(getActivity())
                        .setTitle("提示")
                        .setAlone(true)
                        .setConfirm("我知道了")
                        .setListener(dialog -> mPresenter.init())
                        .setMessage("升级成功");
            }
            if (!updateSuccessDialog.isShowing()) {
                updateSuccessDialog.show();
            }
        }
    }

    @Override
    public void updateFailed(String msg) {
        dialogBuilder.dismiss();
        new MessageDialog.Builder(getActivity())
                .setTitle("提示")
                .setMessage("升级失败" + msg)
                .show();
    }

    @Override
    public void updateFinish() {
        dialogBuilder.dismiss();
    }

    @Override
    public void showAppUpdateDialog(AppDataEntity entity) {
        new MessageDialog.Builder(getActivity())
                .setTitle("更新提示")
                .setMessage(TextUtils.isEmpty(entity.getRemark()) ? "修复已知Bug" : entity.getRemark())
                .setListener(dialog -> mPresenter.downloadApk(entity)).show();
    }

    @Override
    public void startInstall(Intent installAppIntent) {
        startActivity(installAppIntent);
    }

    @Override
    public void showBuildTime(String apkBuildTime) {
        binding.tvBuildTime.setText("构建时间：" + apkBuildTime);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        KeyEventHelper.getInstance().aboutKeyDown(keyCode, () -> binding.tvBuildTime.setVisibility(View.VISIBLE));
        return super.onKeyDown(keyCode, event);
    }

    private void showUpdateBinDialog() {
        new MessageDialog.Builder(getActivity())
                .setTitle("提示")
                .setMessage("确定升级固件版本吗？")
                .setListener(dialog -> mPresenter.updateFirmware()).show();
    }

}